﻿using System;
using System.Collections.Generic;
using SqlSugar;
using WebApplication5.Helper;
using Work3.Models;

namespace WebApplication5.Dao
{
    public class assetssDao
    {
        public static SqlSugarClient db = dbHelper.CretClient();
        public List<assets> SelectAll() {
            return db.Queryable<assets>().ToList();
        }

        public List<assets> Select(int id) {
            return db.Queryable<assets>().Where(it => it.Id == id).ToList();
        }
        
        public List<assets> Select(String owner) {
            return db.Queryable<assets>().Where(it => it.Owner.Contains(owner)).ToList();
        }

        public bool Insert(assets assets) {
            try {
                db.Insertable(assets).IgnoreColumns(false, true).ExecuteCommand();
                return true;
            } catch (Exception e) {
                return false;
            }
        }
        
        public bool Delete(int aid) {
            try {
                db.Deleteable<assets>().In(it => it.Id, aid).ExecuteCommand();
                return true;
            } catch (Exception e) {
                return false;
            }
        }

        public bool Update(assets assets) {
            try {
                db.Updateable(assets).IgnoreColumns(false, true).ExecuteCommand();
                return true;
            } catch (Exception e) {
                return false;
            }
        }
        
        public List<assetsType> showType() {
            return db.Queryable<assetsType>().ToList();
        }

        public bool getIdentity(string username, string department)
        {
            var res = db.Queryable<department>().Where(it => it.Department.Contains(department)).ToList();
            if (res != null)
            {
                if (res[0].Leader.Contains(username))
                {
                    return true;
                }
            }
            return false;
        }
    }
}